package com.guanyong.utils.Cache.EhCahce;

//import com.mortise.cache.ICache;
//import com.mortise.utils.LogHelper;
import com.guanyong.utils.Cache.ICahce.ICache;
import com.guanyong.utils.LogHelper.LogHelper;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import net.sf.ehcache.config.CacheConfiguration;
import org.springframework.stereotype.Service;


/**
 * Created by Eric.Zhang on 2017/3/14.
 */
@Service
public class CacheHelper implements ICache {
    private static Lock lock = new ReentrantLock();// 锁对象
//    private static final String path = "ehcache.xml";

    private static CacheManager manager;

    static   {
        lock.lock();
        try {
//            manager = CacheManager.create(CacheHelper.class.getResourceAsStream(path));
            manager = CacheManager.create();

        }catch (Exception e){
            LogHelper.logger("创建缓存Manager失败："+ e.getMessage());
        }
        finally {
            lock.unlock();
        }
    }



    Cache getCache(String cacheName){
        Cache cache = manager.getCache(cacheName);
        if(cache == null){
            lock.lock();
            try {
                manager.addCache(cacheName);
                cache = manager.getCache(cacheName);
                CacheConfiguration config = cache.getCacheConfiguration();
                config.setOverflowToOffHeap(false);
                config.internalSetOverflowToOffheap(false);
//                config.set
            }
            catch (Exception ex){
                LogHelper.logger("创建缓存对象失败："+ ex.getMessage());

            }
            finally {
                lock.unlock();
            }
//            throw new RuntimeException("根据cacheName="+cacheName+"无法获取到对于的cache!请检查配置文件"+path);
        }
        return cache;
    }

    void _put(String cacheName, String key, Object value, int seconds) {
        Cache cache = getCache(cacheName);
        Element element = new Element(key, value,Integer.MAX_VALUE,seconds);
        cache.put(element);
    }

    @Override
    public void put(String cacheName, String key, Object value) {
       _put(cacheName,key,value,Integer.MAX_VALUE);
    }




    @Override
    public void put(String cacheName, String key, Object value,int seconds) {
        _put(cacheName,key,value,seconds);
    }

    @Override
    public void remove(String cacheName, String key) {
        Cache cache = manager.getCache(cacheName);
        cache.remove(key);
    }

    @Override
    public void remove(String cacheName) {
        manager.removeCache(cacheName);
    }

    @Override
    public Object get(String cacheName, String key) {
        Cache cache = manager.getCache(cacheName);
        if(cache==null){
            return null;
        }
        Element element = cache.get(key);
        if(element==null){
            return null;
        }
        return  element.getObjectValue();
    }

    @Override
    public Map<String,Object> get(String cacheName) {
        Cache cache = manager.getCache(cacheName);
        if(cache==null){
            return null;
        }
        List keys = cache.getKeys();
        if(keys!=null){
            Map<String,Object> result = new HashMap<String,Object>();
            for(int i=0;i<keys.size();i++){
                result.put(String.valueOf( keys.get(i)),cache.get(keys.get(i)));
            }
            return result;
        }
        else {
            return null;
        }
//       return manager.getCacheche(cacheheName);
    }

}
